package com.yjy.shmily;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.yjy.mybase.activity.BaseActivity;
import com.yjy.mybase.http.CustomCallback;
import com.yjy.mybase.http.SuperHttpHelper;
import com.yjy.mybase.model.BaseBean;
import com.yjy.mybase.utils.ARouterUri;
import com.yjy.shmily.account.AccountHelper;

import java.util.HashMap;
import java.util.logging.Handler;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUri.ACTIVITY_LOGIN)
public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    private String mPhone, mPwd;

    @Override
    public int layout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
    }

    @OnClick({R.id.btn_login, R.id.btn_register})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.btn_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
        }
    }

    private void login() {
        mPhone = etPhone.getText().toString().trim();
        mPwd = etPwd.getText().toString().trim();
        if (TextUtils.isEmpty(mPhone) || TextUtils.isEmpty(mPwd)) {
            toast("账号或密码格式不对");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("username", mPhone);
        map.put("password", mPwd);
        SuperHttpHelper.superHttpHelper().get("auth/login", map, new CustomCallback<BaseBean<HashMap<String, String>>>() {
            @Override
            public void success(BaseBean<HashMap<String, String>> hashMapBaseBean) {
                String token = hashMapBaseBean.getResult().get("token");
                AccountHelper.updateToken(token);
                finish();
            }
        });
    }
}
